<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb5">
      <el-col :span="1.5">
        <el-button plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:infor:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:infor:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:infor:remove']">删除</el-button>
      </el-col>
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px"
        class="el-form-search">
        <el-form-item label="ID" prop="id" class="el-form-search-item">
          <el-input v-model="queryParams.id" placeholder="请输入标题" clearable size="mini"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="标题" prop="title" class="el-form-search-item">
          <el-input v-model="queryParams.title" placeholder="请输入标题" clearable size="mini"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="创建人" prop="createor" class="el-form-search-item">
          <el-input v-model="queryParams.createor" placeholder="请输入创建人" clearable size="mini"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime" class="el-form-search-item">
          <el-date-picker clearable size="mini" v-model="queryParams.createTime" type="date" value-format="yyyy-MM-dd"
            placeholder="选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="文章作者" prop="author" class="el-form-search-item">
          <el-input v-model="queryParams.author" placeholder="请输入文章作者" clearable size="mini"
            @keyup.enter.native="handleQuery" />
        </el-form-item>

        <el-form-item class="el-form-search-item">
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-row>

    <el-table ref="list" :height="tableHeight" v-loading="loading" :data="inforList"
      @selection-change="handleSelectionChange" @row-click="handleRowClick" highlight-current-row>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="50" align="center">
        <template scope="scope">
          <span>{{
            (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="文章分类" align="center" prop="extend6" />
      <!-- <el-table-column label="文章分类" align="center" prop="type" /> -->
      <!-- <el-table-column label="内容" align="center" prop="content" /> -->
      <el-table-column label="创建人" align="center" prop="createor" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="文章作者" align="center" prop="author" />

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:infor:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:infor:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改文章对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="false"
      v-dialogDrag>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <editor v-model="form.content" :min-height="192" />
        </el-form-item>
        <el-form-item label="文章分类" prop="type">
          <el-select v-model="form.type" placeholder="请选择下拉选择" clearable :style="{ width: '100%' }">
            <el-option v-for="(item, index) in typeList" :key="index" :label="item.label" :value="item.value"
              :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="创建人" prop="createor">
          <el-input v-model="form.createor" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="文章作者" prop="author">
          <el-input v-model="form.author" placeholder="请输入文章作者" />
        </el-form-item>

        <el-form-item label="缩略图上传" prop="extend7">
          <image-upload v-model="form.extend7" ref="upPic" limit="1" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfor, getInfor, delInfor, addInfor, updateInfor } from "@/api/partyBuilding/article";

export default {
  name: "Infor",
  data() {
    return {
      typeList: [{
        "label": "政策解读",
        "value": 0
      }, {
        "label": "百年党史",
        "value": 1
      }, {
        "label": "特色党建",
        "value": 2
      }, {
        "label": "三会一课",
        "value": 3
      }, {
        "label": "乡村振兴",
        "value": 4
      }, {
        "label": "司机之家",
        "value": 5
      }],
      baseUrl: process.env.VUE_APP_BASE_API,
      // 表格高度
      tableHeight: document.documentElement.clientHeight - 180,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文章表格数据
      inforList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: "create_time",
        isAsc: "desc",
        title: null,
        type: null,
        content: null,
        createor: null,
        createTime: null,
        author: null,
        extend1: null,
        extend2: null,
        extend3: null,
        extend4: null,
        extend5: null,
        extend6: null,
        extend7: null,
        extend8: null,
        id: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "0：政策解读 1：党史百年 2：特色党建 3：三会一课 4：乡村振兴 5： 司机之家不能为空", trigger: "change" }
        ],
        extend7: [
          { required: true, message: "请上传缩略图", trigger: "blur" }
        ],
        content: [
          { required: true, message: "内容不能为空", trigger: "blur" }
        ],
      },

    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询文章列表 */
    getList() {
      this.loading = true;
      listInfor(this.queryParams).then(response => {
        this.inforList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        type: null,
        content: null,
        createor: null,
        createTime: null,
        author: null,
        extend1: null,
        extend2: null,
        extend3: null,
        extend4: null,
        extend5: null,
        extend6: null,
        extend7: null,
        extend8: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 单击行选中数据
    handleRowClick(row, column, event) {
      this.$refs.list.toggleRowSelection(row);
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加文章";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getInfor(id).then(response => {
        let form = response.data;
        let content = form.content;
        if (content != null && content != '') {
          let newHtmlString = content.replace(/<video[^>]*><source src="([^"]+)" type="video\/mp4" \/><\/video>/gi, '<iframe class="ql-video" frameborder="0" allowfullscreen="true" src="$1"></iframe>');
          console.log(newHtmlString);
          form.content = newHtmlString;
          this.form = form;
        }
        this.open = true;
        this.title = "修改文章";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.type == 0) {
            this.form.extend6 = '特色党建'
          } else if (this.form.type == 1) {
            this.form.extend6 = '政策解读'
          } else if (this.form.type == 2) {
            this.form.extend6 = '党史百年'
          } else if (this.form.type == 3) {
            this.form.extend6 = '军人之家'
          } else if (this.form.type == 4) {
            this.form.extend6 = '乡村振兴'
          } else if (this.form.type == 5) {
            this.form.extend6 = '司机之家'
          }

          if (this.form.id != null) {
            console.log()
            // if (this.form.extend7 !== this.$refs.upPic.imgUrl) {
            //   console.log("jinlaille3")
            //   this.form.extend7 = this.$refs.upPic.imgUrl;
            //   }
            updateInfor(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            // this.form.extend7 = this.$refs.upPic.imgUrl;
            addInfor(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除记录？').then(function () {
        return delInfor(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
  }
};
</script>
